package com.sanshow.charging;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.sanshow.charging.db.Charging;
import com.sanshow.charging.db.ChargingDao;
import com.sanshow.charging.fragment.Table1Fragment;
import com.sanshow.charging.fragment.Table2Fragment;
import com.sanshow.charging.util.AppInfoUtils;
import com.sanshow.charging.util.UpdateUtils;

import java.util.List;

public class BackActivity extends AppCompatActivity implements View.OnClickListener {

    FrameLayout mContainer;
    Button mBtnCharging;
    Button mBtnWakeboard;
    Button mBtnAppRestart;
    Button mBtnAppExit;
    ImageView mBtnBack;
    TextView mTvAppVersion;
    TextView mTvBackTitle;
    TextView mTvBackDeviceNo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_back);

        initLayout();
    }

    public static void getInstance(Context context) {
        context.startActivity(new Intent(context, BackActivity.class));
    }

    private void initLayout() {
        mBtnCharging = findViewById(R.id.btn_charging);
        mBtnWakeboard = findViewById(R.id.btn_wakeboard);
        mBtnBack = findViewById(R.id.iv_back);
        mBtnAppRestart = findViewById(R.id.btn_app_restart);
        mBtnAppExit = findViewById(R.id.btn_app_exit);
        mTvAppVersion = this.findViewById(R.id.tv_app_version);
        mBtnCharging.setOnClickListener(this);
        mBtnWakeboard.setOnClickListener(this);
        mBtnBack.setOnClickListener(this);
        mBtnAppRestart.setOnClickListener(this);
        mBtnAppExit.setOnClickListener(this);

        mContainer = findViewById(R.id.table_container);
        getSupportFragmentManager().beginTransaction()
                .add(R.id.table_container, Table1Fragment.newInstance())
                .commit();

        mTvAppVersion.setText("当前版本：" + AppInfoUtils.getVersionName());

        mTvBackTitle = findViewById(R.id.tv_back_title);
        mTvBackDeviceNo = findViewById(R.id.tv_back_device_no);

        String title = SPUtils.getInstance().getString("device_title");
        String deviceNo = SPUtils.getInstance().getString("device_code");
        if (!StringUtils.isEmpty(title)) {
            mTvBackTitle.setText(title);
        }
        if (!StringUtils.isEmpty(deviceNo)) {
            mTvBackDeviceNo.setText(deviceNo);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_charging: {
                mBtnCharging.setBackgroundResource(R.color.btn_back);
                mBtnWakeboard.setBackgroundResource(R.color.btn_default);
                showFragment(Table1Fragment.newInstance());
                break;
            }
            case R.id.btn_wakeboard: {
                mBtnCharging.setBackgroundResource(R.color.btn_default);
                mBtnWakeboard.setBackgroundResource(R.color.btn_back);
                showFragment(Table2Fragment.newInstance());
                break;
            }
            case R.id.iv_back: {
                ChargingDao chargingDao = MyApplication.getDaoInstant().getChargingDao();
                List<Charging> chargingListDoor = chargingDao.queryBuilder().where(ChargingDao.Properties.DoorStatus.eq("01")).list();
                if (chargingListDoor != null && chargingListDoor.size() > 0) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(BackActivity.this, R.style.buttonDialog);
                    builder.setIcon(R.mipmap.back_error)
                            .setTitle("提示")
                            .setMessage("当前还有仓门未关，请先关好仓门，再返回首页！")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                }
                            })
                            .create().show();
                }else {
                    this.finish();
                }
                break;
            }
            case R.id.btn_app_restart: {
                ChargingDao chargingDao = MyApplication.getDaoInstant().getChargingDao();
                List<Charging> chargingList = chargingDao.queryBuilder().where(ChargingDao.Properties.DoorStatus.eq("01")).list();
                if (chargingList != null && chargingList.size() > 0) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(BackActivity.this, R.style.buttonDialog);
                    builder.setIcon(R.mipmap.back_error)
                            .setTitle("提示")
                            .setMessage("请先关好仓门，再重启App")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                }
                            })
                            .create().show();
                } else {
                    SPUtils.getInstance().put("stateData", "");
                    AppUtils.relaunchApp(true);
                }
                break;
            }
            case R.id.btn_app_exit: {
                ChargingDao chargingDao = MyApplication.getDaoInstant().getChargingDao();
                List<Charging> chargingList = chargingDao.queryBuilder().where(ChargingDao.Properties.DoorStatus.eq("01")).list();
                if (chargingList != null && chargingList.size() > 0) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(BackActivity.this, R.style.buttonDialog);
                    builder.setIcon(R.mipmap.back_error)
                            .setTitle("提示")
                            .setMessage("请先关好仓门，再退出App")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                }
                            })
                            .create().show();
                } else {
                    SPUtils.getInstance().put("stateData", "");
                    AppUtils.exitApp();
                }
                break;
            }
        }
    }

    public void showFragment(Fragment fragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.table_container, fragment);
        fragmentTransaction.commit();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            ChargingDao chargingDao = MyApplication.getDaoInstant().getChargingDao();
            List<Charging> chargingListDoor = chargingDao.queryBuilder().where(ChargingDao.Properties.DoorStatus.eq("01")).list();
            if (chargingListDoor != null && chargingListDoor.size() > 0) {
                AlertDialog.Builder builder = new AlertDialog.Builder(BackActivity.this, R.style.buttonDialog);
                builder.setIcon(R.mipmap.back_error)
                        .setTitle("提示")
                        .setMessage("当前还有仓门未关，请先关好仓门，再返回首页！")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .create().show();
            }else{
                this.finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}